package com.k2data.k2app.domain.po;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.Maps;
import com.k2data.k2app.domain.po.v1.Contacts;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author lidong9144@163.com 17-3-16.
 */
@Getter
@Setter
@EqualsAndHashCode(exclude = "customColumnMap")
@ToString(exclude = "customColumnMap")
@Table(name = "device")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Device implements Serializable {

    @Transient
    private static final long serialVersionUID = 8856314231117075894L;

    @JsonIgnore
    @Id
    @GeneratedValue(generator = "JDBC")
    private Long id;
    @NotNull
//    @Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9_]*$", message = "DeviceNo must be letter, number or underscode.")
    private String deviceNo;
    private String pinCode;
    private String type;
    private String spec;
    private String certificateNo;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private ZonedDateTime produceDate;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private ZonedDateTime operationDate;
    private String status;
    @NotNull
    private String name;
    private String fullName;
    private String owner;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private ZonedDateTime latestRepairDate;
    private String remark;
    private Integer isRoot;
    // 模板 ID
    private Long tDeviceId;
    private String equipmentName;
    private String keyName;
    private String imageUrl;
    private String overviewUrl;
    @JsonIgnore
    private ZonedDateTime createTime;
    @JsonIgnore
    private ZonedDateTime updateTime;

    @Transient
    private String dataSource;
    @JsonIgnore
    @Transient
    private Integer count;
    @JsonIgnore
    @Transient
    private String typeEn;
    @JsonIgnore
    @Transient
    private String orderBy;
    @Transient
    private Long customerId;
    @JsonIgnore
    @Transient
    private String customerName;

    // 关联表
    @JsonIgnore
    @Transient
    private TDevice template;
    @JsonIgnore
    @Transient
    private List<DeviceCustomColumn> customColumns;
    @JsonIgnore
    @Transient
    private volatile Map<String, DeviceCustomColumn> customColumnMap;
    @JsonIgnore
    @Transient
    private List<EventFault> faults;

    @Transient
    private Long parentId;
    // 联系人
    @Transient
    private List<Contacts> contacts;
    @Transient
    private List fileList;
    @Transient
    private Long userId;
    /**
     * 根据字段名获得自定义字段的值
     *
     * @param nameEn 字段名
     * @return 字段值
     */
    @JsonIgnore
    @Transient
    public String getCustomValue(String nameEn) {
        String value = "";
        if(customColumns != null && customColumns.size() > 0){
            value = customColumns.get(0).getValue();
        }
        return value;

    }

}
